package net.hzlj.platform.enterprise.widght;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.hzlj.rl.zhangjiajie.R;

import java.util.HashMap;
import java.util.Map;

public class NavTabBottomBar extends ElasticHScrollView {

	private boolean mFinishAdd = true;;
	/** 上下文*/
	private Context mContext;
	/** 当前焦点位置*/
	private int mCurSelected = 0;
	/** 导航栏布局*/
	private LinearLayout mNavLayout;
	/** 指示器*/
//	private ImageView mSelctedIndexImg;
	/** 事件回调*/
	private OnNavTabBarSelectedListener mOnNavTabBarSelectListener;
	/** 文字颜色状态#b4c833*/
//	private int[] mTextColorState = new int[]{0xff585858, 0xff03A9F5};
	/** 焦点背景图片*/
//	private int mSelectedBgRes = R.drawable.transparent;
	private int mSelectedBgRes = R.drawable.transparent;
	/** 是否显示滑动指示器*/
	private boolean mShowIndexImg = false;

	public int totalLength=0;
	
	//private Map<String, View> mNavTabViews = new HashMap<String, View>();
	private Map<String, ImageView> mBadgetViews = new HashMap<String, ImageView>();
	
	public void setCurSelected(int mCurSelected) {
		this.mCurSelected = mCurSelected;
	}
	public LinearLayout getmNavLayout() {
		return mNavLayout;
	}

	public NavTabBottomBar(Context context) {
		super(context);
		init();
	}
	
	public NavTabBottomBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		mContext = getContext();
		if(isInEditMode()){
			return;
		}
		RelativeLayout container = (RelativeLayout)LinearLayout.inflate(mContext, R.layout.widget_bottom_navbar_layout, null);
//		mSelctedIndexImg = (ImageView)container.findViewById(R.id.navbar_selected_index_image);
		mNavLayout = (LinearLayout)container.findViewById(R.id.navbar_layout);
		addView(container);
	}
	
	private void addNavChildView(View view) {
		mNavLayout.addView(view);
		view.setTag(mNavLayout.getChildCount()-1);
		view.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				setSelected((Integer)v.getTag(), true);
			}
		});
	}
	
	private void addNavView(String columnName,ImageView target){
		mBadgetViews.put(columnName, target);
	}
	
	public void showBadgeText(String columnName){
		ImageView view = mBadgetViews.get(columnName);
		if (view !=null) {
			view.setVisibility(View.VISIBLE);
		}
	}
	
	public void hideBadgeView(String columnName){
		ImageView view = mBadgetViews.get(columnName);
		if (view !=null) {
			view.setVisibility(View.GONE);
		}
	}
	
	public void addNavChildText(String text, int dividerRes,int iconResId) {
    	View view = LinearLayout.inflate(mContext,R.layout.widget_bottom_navbar_tab_item,null);
    	TextView tv = (TextView)view.findViewById(R.id.item_text);
    	ImageView imageView = (ImageView) view.findViewById(R.id.item_tip);
    	tv.setText(text);

    	if (iconResId != 0) {
    		tv.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
        }
    	addNavChildView(view);
    	addNavView(text,imageView);
	}

    public void addNavChildText(String text, int dividerRes,int iconResId,boolean isVisible) {
        View view = LinearLayout.inflate(mContext,R.layout.widget_bottom_navbar_tab_item,null);
        TextView tv = (TextView)view.findViewById(R.id.item_text);
        ImageView imageView = (ImageView) view.findViewById(R.id.item_tip);
        tv.setText(text);
//        TextView line = (TextView) view.findViewById(R.id.item_line);
//        if (isVisible){
//            line.setVisibility(VISIBLE);
//        }else{
//            line.setVisibility(GONE);
//        }
        if (iconResId != 0) {
            tv.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
        }
        addNavChildView(view);
        addNavView(text,imageView);
    }
	
	public void finishAdd(int width, int selectedBgRes) {
		if (!mFinishAdd)
			return;
		if (selectedBgRes != 0){
			this.mSelectedBgRes = selectedBgRes;
		}
		mFinishAdd = false;
		int count = mNavLayout.getChildCount();
		if(count == 0)
			return;
		if(getNavChildView(count-1).getRight() < getRight()) {
			for(int i=0;i<count;i++) {
				View view = getNavChildView(i);
				
				TextView tv = (TextView)view.findViewById(R.id.item_text);
                view.setLayoutParams(new LinearLayout.LayoutParams(width/count,LinearLayout.LayoutParams.MATCH_PARENT));

				if (mCurSelected == i) {
			    	tv.setBackgroundResource(mSelectedBgRes);
			    	tv.setSelected(true);
				} else {
					tv.setSelected(false);
			    	tv.setBackgroundResource(0);
				}
				
			}
		}
	}
	
	public void setSelected(int position, boolean callback) {
    	if(mCurSelected == position)
    		return;
    	int lastCurSelected = mCurSelected;
    	
    	mCurSelected = position;
    	for(int i=0;i<mNavLayout.getChildCount();i++) {
    		View view = mNavLayout.getChildAt(i);
    		if(i == position) {
    			TextView tv = (TextView)view.findViewById(R.id.item_text);
    			
    			if (mShowIndexImg){
    			} else{
	    			tv.setSelected(true);
	    			tv.setBackgroundResource(mSelectedBgRes);
    			}
				
				if (callback && mOnNavTabBarSelectListener != null)
					mOnNavTabBarSelectListener.onTabSelected(lastCurSelected,position);
    		}else if(i == lastCurSelected) {
    			view.setSelected(false);
    			TextView tv = (TextView)view.findViewById(R.id.item_text);
    			tv.setSelected(false);
    			tv.setBackgroundResource(0);
    		}
    	}
	}
	
	public int getCurrentSelectedPosition() {
		return mCurSelected;
	}
	
	public View getNavChildView(int position) {
		if(position >= 0 && position < mNavLayout.getChildCount())
			return mNavLayout.getChildAt(position);
		return null;
	}
	
	public void reset(){
		mFinishAdd = true;
	}
	
	/**
	 * NavTabBar事件监听回调类
	 * @author dailiang
	 * @date 2014-4-17
	 * @copyright YTXT
	 * @version 1.0
	 */
	public static interface OnNavTabBarSelectedListener {
		
		/**
		 * 点击导航栏某一项时调用
		 * @param position
		 */
		public void onTabSelected(int position);
		public void onTabSelected(int present, int position);
	}
	
	public void setOnNavTabSelectedListener(OnNavTabBarSelectedListener listener) {
		this.mOnNavTabBarSelectListener = listener;
	}
	
	public boolean isShowIndexImg() {
		return mShowIndexImg;
	}

	public void setShowIndexImg(boolean mShowIndexImg) {
		this.mShowIndexImg = mShowIndexImg;
	}

}
